// const { resolve } = require("node:path")

const run = () => console.log('run')
const look = () => console.log('look')

//依据调用堆栈执行函数
const see = () => {
    console.log('see')
    run()
    look()
}

/*
setTimeout 将回调函数放入消息队列
作业队列
尽快的执行函数而非调用堆栈的末尾
在当前函数结束之前resolve 的 Promise 会在当前函数之后被立即执行
*/
const seeu = () => {
    console.log('see u start')
    setImmediate(() => console.log('setImmediate'))
    setTimeout(() => console.log('setTimeout'), 0)

    new Promise((resolve, reject) => {
        resolve('new Promise [after this function End,before CallStack]')
    }).then(resolve => console.log(resolve))

    process.nextTick(() => console.log('process.nextTick'))
    look()
    console.log('see u end')
}

see()
console.log('--------------')
seeu()
console.log('ALL-END')

//Promisifying 
const fs = require('fs')
const getFile = (fileName) => {
    return new Promise((resolve, reject) => {
        fs.readFile(fileName, (err, data) => {
            if (err) {
                reject(err)
                return
            }
            resolve(data)
        })
    })
}
getFile('./README.MD')
    .then(data => console.log(data))
    .catch(err => console.log(err))

//Promise级联操作
console.log('Promise级联操作')
const p1 = new Promise((resolve, reject) => {
    console.log('exec Promise 001')
    setTimeout(resolve, 180, 'Promise 001')
})
const p2 = new Promise((resolve, reject) => {
    console.log('exec Promise 002')
    setTimeout(resolve, 250, 'Promise 002')
})
const p3 = new Promise((resolve, reject) => {
    console.log('exec Promise 003')
    setTimeout(resolve, 100, 'Promise 003')
})
const p4 = new Promise((resolve, reject) => {
    console.log('exec Promise 004')
    setTimeout(resolve, 80, 'Promise 004')
})

//所有的promise完成后
Promise.all([p1, p2])
    .then(res => {
        console.log(res)
    })
    .catch(err => {
        console.log(err)
    })
//任何一个promise完成后，即开始执行
Promise.race([p3, p4])
    .then(res => {
        console.log(res)
    })
    .catch(err => {
        console.log(err)
    })
//Async Await
const doAsync = () => {
    return new Promise((resolve) => {
        setTimeout(() => resolve('do Async'), 200)
    })
}
const doSync = async () => {
    console.log(await doAsync())
}

console.log('Before doSync')
doSync()
console.log('After doSync')

//对比promise
const fetch = require('node-fetch')
const getFirstUserData = () => {
    return fetch('/package-lock.json') // 获取用户列表
        .then(response => response.json()) // 解析 JSON
        .then(users => users[0]) // 选择第一个用户
        .then(user => fetch(`/users/${user.name}`)) // 获取用户数据
        .then(userResponse => userResponse.json()) // 解析 JSON
}
getFirstUserData()

const getFirstUserData2 = async () => {
    const response = await fetch('/package-lock.json') // 获取用户列表
    const users = await response.json() // 解析 JSON
    const user = users[0] // 选择第一个用户
    const userResponse = await fetch(`/users/${user.name}`) // 获取用户数据
    const userData = await userResponse.json() // 解析 JSON
    return userData
}
getFirstUserData2()

//事件触发器
const EventEmitter = require('events')
const eventEmitter = new EventEmitter()

eventEmitter.on('start',(arg)=>{
    console.log(`Event Start:${arg}`)
})

eventEmitter.emit('start','xx-001')